/*
 * Copyright (C) <2009>  <Matthew Dickinson>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.pulsarwebdevelopment.concordance.plugins;

//import javax.swing.*;
//import javax.swing.event.*;

import com.pulsarwebdevelopment.concordance.Concordance;
import com.pulsarwebdevelopment.concordance.ConcordanceOutput;
import com.pulsarwebdevelopment.concordance.ConcordanceOutputStream;
import com.pulsarwebdevelopment.concordance.Tools;
import com.pulsarwebdevelopment.concordance.Verse;

import java.awt.*;
import java.io.StringWriter;
import java.net.*;
import java.util.Arrays;
//import java.awt.event.*;

/**
 * Allows user to user a live-search on a concordance
 * @author Matthew Dickinson
 * Last updated: 8/13/2009
 */
/*public class LiveSearch extends JPanel implements DocumentListener, Plugin{

	private static final long serialVersionUID = 1L;	//thing
	private JTextField text;
	private JTextArea area;
	private Concordance conc;
	private String concTxt;
	//private String searchTxt;

//	public static void main(String[] args){
//		LiveSearch search = new LiveSearch();
//		search.go(new Object[1]);
//	}

	public LiveSearch(){
		super(new GridBagLayout());

		text = new JTextField(45);
		text.getDocument().addDocumentListener(this);

		area = new JTextArea(30, 45);
		area.setEditable(false);
		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		JScrollPane scrollPane = new JScrollPane(area);

		URL url = this.getClass().getResource("search.png");
		JLabel icon = new JLabel(new ImageIcon(url));

		//Add Components to this panel.
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = 1;
		add(icon, c);

		c.gridwidth = GridBagConstraints.REMAINDER;

		c.fill = GridBagConstraints.HORIZONTAL;
		add(text, c);

		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(scrollPane, c);
	}

	public void changedUpdate(DocumentEvent arg0) {
		System.out.println("change");
		getOccurences(text.getText());
	}

	public void insertUpdate(DocumentEvent arg0) {
		System.out.println("Insert");
		getOccurences(text.getText());
	}

	public void removeUpdate(DocumentEvent arg0){
		System.out.println("Remove");
		getOccurences(text.getText());
	}

	private void getOccurences(String pattern){
		StringWriter output = new StringWriter();
		ConcordanceOutput<String, Verse> outputStream = 
			new ConcordanceOutputStream<String, Verse>(output, ConcordanceOutputStream.ListStyle.SINGLE_LINE_TRUNCATED);
		int lastIndexOfSpace = pattern.lastIndexOf(' ');
		if(lastIndexOfSpace < 1 || lastIndexOfSpace == (pattern.length() -1)){
			conc.getMatchingWordSet(Tools.capitalize(pattern.trim()), outputStream);
			concTxt = output.toString();
		}else{
			String[] patterns = pattern.split(" ");
			String[] pattList = new String[patterns.length];
			for(int i = 0; i < patterns.length; i++){
				pattList[i] = Tools.capitalize(patterns[i]);
			}
			System.out.println("Patterns: " + Arrays.toString(pattList));
			conc.getMultipleMatchingWordSet(outputStream, pattList);
			concTxt = output.toString();
		}
		paint();
	}
	
	public void paint(){
		area.setText(concTxt);
		area.setCaretPosition(0);	//set the display field back to the top
	}

	public void go(Object[] args) {
		int concIndex = (Integer) args[0];
		conc = new Concordance(concIndex);
		
		JFrame bob = new JFrame("Concordance Search");
		bob.add(this);
		paint();

		bob.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		bob.pack();
		bob.setVisible(true);		
	}

	public void plug(PluginScreen screen) {
		screen.setTitleText("Live Search", "This plug-in will allow you to search for any word and will update the search results as you type");
		screen.giveConcList("Choose the concordance that you wish to search");
	}

}*/
